package yue.shen.android.demo.select

import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.luck.picture.lib.entity.LocalMediaFolder
import com.luck.picture.lib.tools.ScreenUtils
import com.luck.picture.lib.tools.StringUtils
import yue.shen.android.demo.R


class SelectFolderPop(private val context: Context, private val pictureTitle: TextView) :
    PopupWindow(context) {

    private var animationIn: Animation? = null
    private var animationOut: Animation? = null
    private var drawableUp: Drawable? = null
    private var drawableDown: Drawable? = null
    private var isDismiss = false
    private var recyclerView: RecyclerView? = null
    private val adapter = SelectFolderAdapter(ArrayList())

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.select_folder_pop, null)
        this.contentView = view
        this.width = ScreenUtils.getScreenWidth(context)
        this.height = ScreenUtils.getScreenHeight(context)
        this.animationStyle = R.style.WindowStyle
        this.isFocusable = true
        this.isOutsideTouchable = true
        this.update()
        this.setBackgroundDrawable(ColorDrawable(Color.argb(123, 0, 0, 0)))
        drawableUp = context.resources.getDrawable(R.mipmap.orange_arrow_up, null)
        drawableDown = context.resources.getDrawable(R.mipmap.orange_arrow_down, null)
        animationIn = AnimationUtils.loadAnimation(context, R.anim.photo_album_show)
        animationOut = AnimationUtils.loadAnimation(context, R.anim.photo_album_dismiss)
        initView(view)
    }

    private fun initView(view: View) {
        view.findViewById<LinearLayout>(R.id.id_ll_root).setOnClickListener { dismiss() }
        recyclerView = view.findViewById(R.id.folder_list)
        recyclerView?.layoutManager = LinearLayoutManager(context)
        recyclerView?.layoutParams?.height = (ScreenUtils.getScreenHeight(context) * 0.6).toInt()
        recyclerView?.adapter = adapter
    }

    override fun showAsDropDown(anchor: View) {
        try {
            if (Build.VERSION.SDK_INT >= 24) {
                val rect = Rect()
                anchor.getGlobalVisibleRect(rect)
                val h: Int = anchor.resources.displayMetrics.heightPixels - rect.bottom
                height = h
            }
            super.showAsDropDown(anchor)
            isDismiss = false
            recyclerView?.startAnimation(animationIn)
            StringUtils.modifyTextViewDrawable(pictureTitle, drawableUp, 2)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun dismiss() {
        if (isDismiss) {
            return
        }
        StringUtils.modifyTextViewDrawable(pictureTitle, drawableDown, 2)
        isDismiss = true
        recyclerView?.startAnimation(animationOut)
        dismiss()
        animationOut?.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationEnd(animation: Animation) {
                isDismiss = false
                Handler().post { super@SelectFolderPop.dismiss() }
            }

            override fun onAnimationRepeat(animation: Animation) {}
        })
    }

    fun bindFolder(folders: ArrayList<LocalMediaFolder>) {
        adapter.bindFolderData(folders)
    }

    fun setOnItemClickListener(listener:SelectFolderListener) {
        adapter.setOnItemClickListener(listener)
    }

}